package fsu.cs.cis5930;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;

public class Bomb extends ImageView {
	enum Type
	{
		EMPTY,	// no bomb
		NORMAL, // default bomb
		FAST,	// TODO: explodes 33% faster
		MOVING 	// TODO: moves to an adjacent square every second
	}
	
	static final public 	long 	DEFAULT_FUSE 	= 3000;
	
	private Type 	bombType;	// type of the bomb
	private Timer 	fuse;		// time before bomb explodes
	private Handler explodeBomb;
	private Animation fadeIn;
	private Context mContext;
		//default constructor
		public Bomb(Context context)
		{	this(
				context,
				Type.NORMAL,
				DEFAULT_FUSE );}
		
		public Bomb(Context context, Type argType)
		{	this(
				context,
				argType,
				DEFAULT_FUSE );	}
		
		public Bomb(Context context, Type argType, long argFuse)
		{
			super(context);
			mContext = context;
			bombType = argType;
			
			if (bombType != Type.EMPTY)
			{
				fuse = new Timer();
				fuse.schedule( new explodeBomb(), argFuse);
			}
			
			switch (bombType)
			{
			case EMPTY:
				this.setImageResource(R.drawable.blank);
				break;
			default:
				this.setImageResource(R.drawable.bomb);
				break;
			}
					
			setScaleType(ScaleType.FIT_XY);

			final Bomb theBomb = this;
			explodeBomb = new Handler() { 
	        	@Override public void handleMessage(Message msg) { theBomb.setImageResource(R.drawable.boom);}
	        };

		}
		
		public Type getType()
		{ return bombType; }
		
		
		public void diffuse()
		{
			if ( fuse != null)
				fuse.cancel();
			this.setImageResource(R.drawable.blank);
			bombType = Type.EMPTY;
		}
		
		  private class explodeBomb extends TimerTask {
		        public void run() {
		            GameEngine engine =  (GameEngine) mContext.getApplicationContext();
		    	    engine.endGame();
		        	explodeBomb.sendEmptyMessage(0);
		            fuse.cancel(); //Terminate the timer thread
		            
		        }
		    }
	}

